package leesoft.hub.actions;

import java.util.Map;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.*;
import leesoft.hub.actions.SessionContext;


public class SetCookieAction implements IAction {
	HttpServletResponse response;
	HttpServletRequest request;
	public Map process(SessionContext ctx) {
		String value=ctx.getParameterString("value");
	
		if(value.equals("")){
			String areacode="";
			Cookie[] cookies=request.getCookies();
			for(Cookie co:cookies){
				if(co.getName().equals("areacode")){
					areacode=co.getValue();
					break;
				}
			}
			HashMap<String,String> returnMap=new HashMap<String,String>();
			returnMap.put("areacode", areacode);
			SessionContext.getInstance(request).put("areacode", areacode);
			return returnMap;
		}

		Cookie areaCookie=new Cookie("areacode",value);
		areaCookie.setMaxAge(60*60*24*365);
		response.addCookie(areaCookie);
		SessionContext.getInstance(request).put("areacode",value);
		return null;
	}
	public void setRequest(HttpServletRequest request){
		this.request=request;
	}
	public void setResponse(HttpServletResponse response){
		this.response=response;
	}

}
